package com.dq.service.sys.user.impl;

import com.dq.controller.in.base.BaseAdminQuery;
import com.dq.controller.in.user.SysUserQuery;
import com.dq.domain.base.impl.BaseDaoImpl;
import com.dq.domain.sys.role.SysUserRole;
import com.dq.exception.customException.AdminException;
import com.dq.repository.BaseRepositories;
import com.dq.service.sys.user.SysUserRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

/**
 * @author cf
 */
@Service
public class SysUserRoleServiceImpl extends BaseRepositories implements SysUserRoleService {

    @Override
    public List<SysUserRole> list(SysUserQuery sysUserQuery, List<String> roleCodes) throws AdminException {
        Map<String, Object> query = createQuery(sysUserQuery, roleCodes);

        return dao.find(dao.query2Hql(SysUserRole.class, query), query);
    }
    @Override
    public Long count(SysUserQuery sysUserQuery, List<String> roleCodes) throws AdminException {
        Map<String, Object> query = createQuery(sysUserQuery, roleCodes);

        return dao.count(dao.count2Hql(SysUserRole.class, query), query);
    }
    public Map<String, Object> createQuery(SysUserQuery sysUserQuery, List<String> roleCodes){
        // TODO: 2020/8/3 0003 测试
        //sysUserQuery.setOrgId(0L);
        Map<String, Object> query = sysUserQuery.parseMap();
        query.put("sysUser_deleted", false);
        if(roleCodes != null && roleCodes.size() > 0) {
            query.put("sysRole_code" + BaseDaoImpl.inSqlStr, roleCodes);
        }
        if(sysUserQuery.getOrgId() != null&&sysUserQuery.getOrgId() !=0) {
            query.put("sysUser_section_id", sysUserQuery.getOrgId());
            query.remove("orgId");
        }
        if (sysUserQuery.getOrgId() != null&&sysUserQuery.getOrgId() ==0){
            query.remove("orgId");
        }

        if(StringUtils.isNotBlank(sysUserQuery.getMobile())) {
            query.put("sysUser_mobile", sysUserQuery.getMobile());
            query.remove("mobile");
        }
        if(StringUtils.isNotEmpty(sysUserQuery.getUsername())){
            query.put("sysUser_username" + BaseDaoImpl.likeSqlStr, BaseAdminQuery.likeSql(sysUserQuery.getUsername()));
            query.remove("username");
        }
        if(StringUtils.isNotBlank(sysUserQuery.getUserRealcName())) {
            query.put("sysUser_userRealcName" + BaseDaoImpl.likeSqlStr, BaseAdminQuery.likeSql(sysUserQuery.getUserRealcName()));
            query.remove("userRealcName");
        }
        if(sysUserQuery.getStatus() != null) {
            query.put("sysUser_status", sysUserQuery.getStatus());
            query.remove("status");
        }
        if(StringUtils.isNotBlank(sysUserQuery.getSectionName())) {
            query.put("sysUser_section_simpleName" + BaseDaoImpl.likeSqlStr, BaseAdminQuery.likeSql(sysUserQuery.getSectionName()));
            query.remove("sectionName");
        }
        if(sysUserQuery.getCreateUser() != null) {
            query.put("sysUser_createUser", sysUserQuery.getCreateUser());
            query.remove("createUser");
        }

        query.put(BaseDaoImpl.HQL_GROUP, "u.sysUser.id");
        query.put("sidx", "sysUser.id");
        query.put("order", "desc");

        return query;
    }

}
